#include "commonpage.h"
#include "ui_commonpage.h"
#include"qqmusic.h"
#include"listitembox.h"
//该方法负责将歌曲信息更新到界面


void CommonPage::setMusicListType(PageType pageType)
{
    this->pageType=pageType;
}
//公共页面的UI设置
void CommonPage::setCommonPageUI(const QString &title, const QString &image)
{
    //设置标题
    ui->pageTitle->setText(title);
    //
    ui->musicImageLabel->setPixmap(QPixmap(image));
    ui->musicImageLabel->setScaledContents(true);
}

CommonPage::CommonPage(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::CommonPage)
{
    ui->setupUi(this);
    ui->pageMusicList->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
    connect(ui->playAllBtn, &QPushButton::clicked, this, [=](){ emit playAll(pageType);  });
    connect(ui->pageMusicList, &QListWidget::doubleClicked, this, [=](const QModelIndex &index){
     // ⿏标双击后，发射信号告诉QQMusic，播放this⻚⾯中共被双击的歌曲
     emit playMusicByIndex(this, index.row());
});

}

void CommonPage::addMusicToPlayer(MusicList &musicList, QMediaPlaylist *playList)
{
    for (auto music:musicList)
    {
        switch (pageType) {
        case LOCAL_PAGE:
            playList->addMedia(music.getMusicUrl());
            break;
        case LIKE_PAGE:
            if(music.getIsLike())
            {
               playList->addMedia(music.getMusicUrl());
            }
            break;
        case HISTORY_PAGE:
            if(music.getIsHistory())
            {
                playList->addMedia(music.getMusicUrl());
            }
        default:
            break;
        }

    }
}

const QString &CommonPage::getMusicIdByIndex(int index) const
{
    qDebug() << "getMusicIdByIndex " << index;
    if(index >= musicListOfPage.size())
     {
        qDebug()<<"⽆此歌曲";
        return " ";
     }
    return musicListOfPage[index];
}

void CommonPage::setMusicImage(QPixmap pixmap)
{
    ui->musicImageLabel->setPixmap(pixmap);
    ui->musicImageLabel->setScaledContents(true);
}

CommonPage::~CommonPage()
{
    delete ui;
}

void CommonPage::addMusicToMusicPage(MusicList &musicList)
{
    //将旧内容清空
    musicListOfPage.clear();
    for(auto& music : musicList)
    {
        switch (pageType) {
            case LOCAL_PAGE:
                musicListOfPage.push_back(music.getMusicId());
                break;

            case LIKE_PAGE:
             {
                if(music.getIsLike())
                {
                    musicListOfPage.push_back(music.getMusicId());
                }
                break;
             }
            case HISTORY_PAGE:
            {
                if(music.getIsHistory())
                {
                    musicListOfPage.push_back(music.getMusicId());
                    break;
                }
            }
            default:
            break;
        }
    }

}
void CommonPage::reFresh(MusicList &musicList)
{


     ui->pageMusicList->clear();
     addMusicToMusicPage(musicList);
     // 遍历歌单，将歌单中的歌曲显⽰到界⾯
      for(auto musicId : musicListOfPage)
      {
        auto it = musicList.findMusicById(musicId);
        if(it == musicList.end())
            continue;
        //将ListItemBox对象放置pageMusicList界面
        ListItemBox* listItemBox = new ListItemBox(ui->pageMusicList);
        //将music的歌曲名称，作者，专辑名称更新到界面
        listItemBox->setMusicName(it->getMusicName());
        listItemBox->setSinger(it->getSingerName());
        listItemBox->setAlbumName(it->getAlbumName());
        listItemBox->setLikeMusic(it->getIsLike());

        QListWidgetItem* listWidgetItem = new QListWidgetItem(ui->pageMusicList);
        listWidgetItem->setSizeHint(QSize(ui->pageMusicList->width(), 45));
        ui->pageMusicList->setItemWidget(listWidgetItem, listItemBox);

        connect(listItemBox, &ListItemBox::setIsLike, this, [=](bool isLike){
            it->setIsLike(isLike);
         emit updateLikeMusic(isLike, it->getMusicId());
         });

      }
      // 更新完成后刷新下界⾯
     ui->pageMusicList->repaint();
}

